package cn.com.blueInfo.bpm.person.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.person.entity.Post;
import cn.com.blueInfo.bpm.person.mapper.PostMapper;
import cn.com.blueInfo.bpm.person.repository.PostRepository;
import cn.com.blueInfo.bpm.person.request.PostDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 岗位与组织关系表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:28:09
 */
@Log4j2
@Service
public class PostRepositoryImpl
        extends ServiceImpl<PostMapper, Post>
        implements PostRepository, BaseRepository<Post, PostDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增岗位与组织关系
     * @param post 岗位与组织关系参数
     */
    @Override
    public Integer createPost(Post post) {
        return sqlExecuteResult(save(post));
    }

    /**
     * 批量新增岗位与组织关系
     * @param postList 岗位与组织关系参数列表
     */
    @Override
    public Integer batchCreatePost(List<Post> postList) {
        return sqlExecuteResult(saveBatch(postList), postList.size());
    }

    /**
     * 删除岗位与组织关系
     * @param postId 岗位与组织关系ID
     */
    @Override
    public Integer deletePost(Long postId) {
        Post post = new Post();
        post.setId(postId);
        post.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(post));
    }

    /**
     * 批量删除岗位与组织关系
     * @param postIdList 岗位与组织关系ID列表
     */
    @Override
    public Integer batchDeletePost(List<Long> postIdList) {
        List<Post> postList = new ArrayList<>();
        for (Long postId : postIdList) {
            Post post = new Post();
            post.setId(postId);
            post.setDeleted(BaseDeleted.DELETED);
            postList.add(post);
        }
        return sqlExecuteResult(removeBatchByIds(postList), postList.size());
    }

    /**
     * 更新部分岗位与组织关系
     * @param post 岗位与组织关系参数
     */
    @Override
    public Integer updatePostPartial(Post post) {
        validateUpdateId(post.getId());
        LambdaUpdateWrapper<Post> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Post::getId, post.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部岗位与组织关系
     * @param post 岗位与组织关系参数
     */
    @Override
    public Integer updatePost(Post post) {
        validateUpdateId(post.getId());
        return sqlExecuteResult(updateById(post));
    }

    /**
     * 查询一条岗位与组织关系
     * @param postId 岗位与组织关系ID
     */
    @Override
    public Post getPostById(Long postId) {
        LambdaQueryWrapper<Post> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Post::getId, postId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询岗位与组织关系
     * @param postQueryDTO 岗位与组织关系参数
     */
    @Override
    public Page<Post> getPostPage(PostDTO.QueryDTO postQueryDTO) {
        Page<Post> postPage = new Page<>(
                postQueryDTO.getPage(), postQueryDTO.getSize());
        LambdaQueryWrapper<Post> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, postQueryDTO);

        BaseDeleted deleted = postQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(postPage, queryWrapper);
        } else {
            return page(postPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 岗位与组织关系 数量
     * @param postQueryDTO 岗位与组织关系参数
     */
    @Override
    public Long getPostCount(PostDTO.QueryDTO postQueryDTO) {
        LambdaQueryWrapper<Post> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, postQueryDTO);

        BaseDeleted deleted = postQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 岗位与组织关系
     * @param postQueryDTO 岗位与组织关系参数
     */
    @Override
    public List<Post> getPostList(PostDTO.QueryDTO postQueryDTO) {
        LambdaQueryWrapper<Post> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, postQueryDTO);

        BaseDeleted deleted = postQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<Post> queryWrapper, PostDTO.QueryDTO postQueryDTO) {

        Long postDefId = postQueryDTO.getPostDefId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(postDefId), Post::getPostDefId, postDefId);

        Long orgId = postQueryDTO.getOrgId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(orgId), Post::getOrgId, orgId);

        addQueryWrapper(queryWrapper, postQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<Post, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                Post::getOrderNumber,
                Post::getTenantId,
                Post::getApplicationId,
                Post::getStatus,
                Post::getDeleted,
                Post::getDeleteUserId,
                Post::getDeleteTime,
                Post::getRemark,
                Post::getCreateUserId,
                Post::getCreateTime,
                Post::getUpdateUserId,
                Post::getUpdateTime,
                Post::getExtend1,
                Post::getExtend2,
                Post::getExtend3);
    }

}
